<template>
  <div class="register">
    <el-container>
      <el-card class="form-card">
        <el-form :model="registerForm" ref="registerForm" :rules="rules">
          <h3>找回密码</h3>
          <el-form-item prop="username">
            <el-input placeholder="请输入用户名" v-model="registerForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入新密码" v-model="registerForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item prop="email">
            <el-input placeholder="请输入邮箱" v-model="registerForm.email"></el-input>
          </el-form-item>
          <el-form-item prop="code" class="identifyCode">
            <el-input placeholder="请输入验证码" v-model="registerForm.code"></el-input>
            <div>
              <el-button :disabled="isSend" @click="sendEmail" type="primary" v-text="buttonTxt"></el-button>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('registerForm')">提交</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="$router.push('/')">返回</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-container>

  </div>
</template>

<script>
import {Get, Post} from "@/plugins/axios";

export default {
  components: {},
  data() {
    return {
      buttonTxt: '发送验证码',
      isSend: false,
      count: 60,
      timer: null,
      registerForm: {
        username: '',
        password: '',
        email: '',
        code: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 4, max: 4, message: '长度为 4 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  mounted() {
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          Post('/user/forget', this.registerForm)
              .then(function (res) {
                if (res.data.code === 200) {
                  _this.$message({
                    showClose: true,
                    message: res.data.message,
                    type: 'success',
                    center: true
                  });

                  _this.$router.push('/');
                } else {
                  _this.$confirm(res.data.message, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                  })
                }
              })
        } else {
          this.$message({
            showClose: true,
            message: "提交错误",
            type: 'error',
            center: true
          });
          return false;
        }
      });
    },
    sendEmail() {
      const pattern = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/
      //正则表达式匹配邮箱格式
      if (pattern.test(this.registerForm.email) === true) {
        Get('/user/sendEmail',
            {email: this.registerForm.email})
            .then(res => {
              if (res.data.code === 200) {
                this.$message({
                  showClose: true,
                  message: '发送成功',
                  type: 'success',
                  center: true
                });

                this.isSend = true
                this.count = 60
                this.timer = window.setInterval(() => {
                  if (this.count === 0) {
                    window.clearInterval(this.timer)
                    this.buttonTxt = '发送验证码'
                    this.isSend = false
                  } else {
                    this.count--;
                    this.buttonTxt = `${this.count}秒后可重新发送`
                  }
                }, 1000);
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.message,
                  type: 'error',
                  center: true
                });
              }
            })
      } else {
        this.$message({
          showClose: true,
          message: "邮箱格式不正确",
          type: 'error',
          center: true
        });
      }

    },
  }
}
</script>

<style lang="scss" scoped>

.el-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login-bg.jpg") no-repeat center/cover;
}

h3 {
  margin: 16px;
}

.form-card {
  width: 400px;
  background: #e0c8d1;
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .5);
}

.identifyCode {
  .el-input {
    width: 180px;
    float: left;
  }

  div {
    float: right;
  }
}

.sentBtn {
  background-color: white;
  color: gainsboro;
}

.el-button {
  width: 100%;
}
</style>